// pages/wangluojiaofei/wangluojiaofei.js

const urlAdd = require('../../utils/config').urlAdd;
const app = getApp();
const stuCode = wx.getStorageSync('stuCode')
Page({

  /**
   * 页面的初始数据
   */
  data: {
    items: [
      { name: '10', value: '10', checked: 'true' },
      { name: '20', value: '20' },
      { name: '50', value: '50' },
      { name: '100', value: '100' },
    ],
    //radio携带的值
    radioMoney: '',
    cardinfoLists:'',
  },

  // 选择金额
  radioChange: function (e) {
    var that = this;
    var radioMoney = e.detail.value;

    that.setData({
      radioMoney: radioMoney,
    })

  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

    wx.setNavigationBarTitle({
      title: "网费缴纳",
    });
    var that = this;


  },

  getAccountBalanceByStuCode: function(){
    var that = this;
    wx.showLoading({
      title: '加载中...',
    });
    wx.request({
      url: urlAdd + '/m/tcommbalanceController/getAccountBalanceByStuCode?stuCode=' + wx.getStorageSync('stuCode'),
      method: 'GET',
      header: {
        'content-type': 'application/json'
      },
      success: function (res) {
        wx.hideLoading();
        var list = res.data.data;
        console.log(list);
        if (list == null) {
          var toastText = '数据获取失败';
          wx.showToast({
            title: toastText,
            image: '/images/erron.png',
            duration: 2000
          });
        } else {
          that.setData({
            cardinfoLists: list
          })

        }
      }
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
// 判断是否授权
var that = this;
if (wx.getStorageSync('openId') != '') {
  if (wx.getStorageSync('stuCode') == '' || wx.getStorageSync('stuCode') == null) {
           wx.showToast({
             title: '你已授过权, 请登录',
           })
           wx.redirectTo({
             url: '../../pages/login/login?openId=' + wx.getStorageSync('openId')
           })
         } else {
           
           that.getAccountBalanceByStuCode();
         }
       } else {
         wx.showModal({
           title: '提示',
           content: '你还未授权登录，是否授权登录',
           cancelText: '取消',
           cancelColor: 'red',
           confirmText: '确认',
           confirmColor: '#30a1db',
           success: function (res) {
             //点击取消
             if (res.cancel) {
               // wx.showModal({
               //   title: '提示',
               //   content: '你取消了授权，功能将无法使用，请重新授权',
               //   showCancel: false,
               //   // cancelText: '确认',
               //   // cancelColor: 'red',
               //   confirmText: '去授权',
               //   confirmColor: '#30a1db',
               //   success: function (res) {
               //     wx.redirectTo({
               //       url: '../../pages/userlogin/login'
               //     })
               //   },
               // })
               wx.switchTab({
                 url: '../../pages/index/index',
               })
             } else {
               //点击确定
               wx.redirectTo({
                 url: '../../pages/userlogin/login'
               })
             }

           },
           fail: function () {}
         })
       }
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },

  //提交表单
  formSubmit: function (e) {
    let { balance, password } = e.detail.value;
    if ( !balance || balance == 0) {
      wx.showModal({
        title: '提示',
        content: '请输入充值金额',
        showCancel: false,
      })
     return;
    } else if(!password){
      wx.showModal({
        title: '提示',
        content: '请输入一卡通密码',
        showCancel: false,
      })
     return;
    } else {
        wx.showModal({
          title: '提示',
          content: '确认缴费',
          showCancel: true,
          success: function(res) {
            wx.request({
              url: urlAdd + '/m/tcommbalanceController/getAccountBalanceByStuCode?stuCode=' + wx.getStorageSync('stuCode'),
              method: 'GET',
              header: {
                'content-type': 'application/json'
              },
              success: function(res) {
                if(res.data.data.blnBalance < balance){
                  wx.showModal({
                    title: '提示',
                    content: '一卡通余额不足! ',
                    showCancel: false,
                    success: function(res) {},
                  })
                }else{
                  wx.request({
                    url: urlAdd + ' ',
                    method: 'GET',
                    header: {
                      'content-type': 'application/json'
                    },
                    success: function(res) {
                      
                    },
                    fail: function(res) {},
                    complete: function(res) {},
                  })
                }
              },
              fail: function(res) {},
              complete: function(res) {},
            })
          },
          fail: function(res) {},
          complete: function(res) {},
        })
    }
    
   },

})